 class NotifyActions {	
 	constructor() {
 	}
	queue = {};
}
var notify = new NotifyActions();
/*
功能: 遍历队列，根据具体标记通知监听者触发回调。使用时需要先add，不需要时一定要remove。
flag: 自定义标记，
callAction: 触发的回调事件
caller: 监听者。同一个标记可以有多个caller
*/
module.exports = {
	notify: (flag, params)=> {
		if (flag) {
			var list = notify.queue[flag];
			if (!list) {return};
			for (var i = 0; i < list.length; i++) {
				var action = list[i].action;
				action && action(params);
			}
		}
	},
	addNotifier:(flag, callAction, caller)=> {
		if (!flag || !callAction || !caller) {
			return;
		};
		var list;
		
		list = notify.queue[flag];
		if (!list) {list = []};
		
		for (var i = 0; i< list.length; i++) {
			if (list[i].action == callAction && list[i].caller == caller) {//已经存在的话，不必重复
				return;
			}
		}
		list.push({action: callAction, caller: caller});
		notify.queue[flag] = list;//保存
		
	},
	removeNotifier: (flag, callAction, caller)=> {
		if (!flag || !callAction || !caller) {
			return;
		};
		
		var list = notify.queue[flag];
		if (!list) {return};
		for (var i = 0; i< list.length; i++) {
			if (list[i].action == callAction && list[i].caller == caller) {
				list.splice(i, 1);
				break;
			};
		}
		if (list.length == 0) {
			delete notify.queue[flag];
		} else {
			notify.queue[flag] = list;
		}
	}
		
	
}